package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import com.jt.vo.PageResult;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@CrossOrigin
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("login")
    public SysResult login(@RequestBody User user){
        String token = userService.findUserByUP(user);
        if(token == null || "".equals(token)){
            //表示用户名和密码错误
            return SysResult.fail();
        }
        //表示用户名和密码正确,返回秘钥信息
        return SysResult.success(token);
        }
/*利用分页查询用户信息
* URL：、user/list
*  返回值 SysResult 对象
*  */
        @GetMapping("/list")/* @RequestBody 只用在post和out请求*/
        public SysResult getUser( PageResult pageResult){
            pageResult = userService.getUserListByPage(pageResult);
            return SysResult.success(pageResult);
        }
        @PutMapping("status/{id}/{status}")
            public SysResult updata(User user){
            System.out.println(user.getStatus());
            userService.updata(user);
            return SysResult.success();
        }

        @PostMapping("addUser")
    public SysResult addUser(@RequestBody  User user){
        userService.saveUser(user);
        return SysResult.success();
    }
    @GetMapping("{id}")
    public SysResult findById(@PathVariable Integer id){
             User user = userService.findById(id);
        return SysResult.success(user);
        }

    @PutMapping("updateUser")
    public SysResult updateUser(@RequestBody User user){
            userService.updateUser(user);
            return SysResult.success();
     }

    @DeleteMapping("{id}")
    public SysResult delete(@PathVariable Integer id){
            userService.findDelete(id);
            return SysResult.success();
    }
}

